<?php

/**
 * @Author: Wang Chunsheng 2192138785@qq.com
 * @Date:   2020-03-08 03:21:14
 * @Last Modified by:   Wang Chunsheng 2192138785@qq.com
 * @Last Modified time: 2020-03-27 19:59:14
 */


namespace common\addons\diandi_shop\backend\setting;

use Yii;
use common\addons\diandi_shop\models\DdDiandiShopStore;
use common\addons\diandi_shop\models\searchs\DdDiandiShopStoreSearch;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use backend\controllers\BaseController;

/**
 * StoreController implements the CRUD actions for DdDiandiShopStore model.
 */
class StoreController extends BaseController
{
    /**
     * @inheritdoc
     */
    public function actions()
    {
        return [
            'setting' => [
                'class' => \yii2mod\settings\actions\SettingsAction::class,
                'view' => 'store',
                // also you can use events as follows:
                'on beforeSave' => function ($event) {
                    $form = $event->getForm();
                    if ($form->surroundings) {
                        $form->surroundings = implode(',', $form->surroundings);
                    }
                    if ($form->certificate) {
                        $form->certificate = implode(',', $form->certificate);
                    }
                },
                'on afterSave' => function ($event) {
                    // your custom code
                },
                'modelClass' => \common\addons\diandi_shop\models\forms\DiandiShopStroe::class,
            ],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * Lists all DdDiandiShopStore models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new DdDiandiShopStoreSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single DdDiandiShopStore model.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new DdDiandiShopStore model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new DdDiandiShopStore();

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        }

        return $this->render('create', [
            'model' => $model,
        ]);
    }

    /**
     * Updates an existing DdDiandiShopStore model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        }

        return $this->render('update', [
            'model' => $model,
        ]);
    }

    /**
     * Deletes an existing DdDiandiShopStore model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the DdDiandiShopStore model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return DdDiandiShopStore the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = DdDiandiShopStore::findOne($id)) !== null) {
            return $model;
        }

        throw new NotFoundHttpException('The requested page does not exist.');
    }
}
